/**
 * 
 */
package test;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import process.TagClusterEnsemble;
import process.TagClustering;
import process.TagSkyline;
import util.DBConnect;

/**
 * The experiment class, test the single K-means, skyline clustering, and clustering algorithm fusion.
 * @author yexijiang
 * @date Jul 10, 2008
 */
public class Experiment 
{
	private String m_ResultUrl = "jdbc:mysql://localhost:3306/tag_clustering";
	private String m_Usr = "root";
	private String m_Pwd = "";
	
	private int m_Sequence[];
	private int m_NumOfCluster;
	private int m_NumOfCandidateTags;
	private int m_RecordsForEachTag;
	
	public Experiment(int sequence[], int numOfCluster, int numOfCandidateTags, int recordsForEachTag)
	{
		m_Sequence = sequence;
		m_NumOfCluster = numOfCluster;
		m_NumOfCandidateTags = numOfCandidateTags;
		m_RecordsForEachTag = recordsForEachTag;
	}

	private void KmeansExperiment()
	{
		TagClustering clustering = new TagClustering(m_Sequence, m_NumOfCluster, m_NumOfCandidateTags, m_RecordsForEachTag);
		Vector<String>[][] clusters = clustering.doClustering();
		
		//	insert into database
		Connection conn = DBConnect.connect(m_ResultUrl, m_Usr, m_Pwd);
		Statement stmt = null;
		
		final String tableName = "tag_clustering_kmeans";
		Vector<String> targetTags = clustering.getTargetTags();
		
		try{
			stmt = conn.createStatement();
			
			
			for(int i = 0; i < clusters.length; ++i)	// for each target tag
			{
				String targetTag = targetTags.get(i);
				for(int j = 0; j < clusters[i].length; ++j)	// for each cluster
				{
					String tags = "";
					for(String tag : clusters[i][j])
					{
						tags += tag + " ";
					}
					String insert = "insert into tag_clustering_kmeans(target_tag, cluster_id, cluster_tags, candidate_tag_number) values('"
						+ targetTag +"'," + j + ",'" + tags + "'," + m_RecordsForEachTag + ");";
//					System.out.println("kmeans:" + insert);
					stmt.executeUpdate(insert);
				}
			}
			conn.close();
		} catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
	}
	
	private void ClusteringEnsembleExperiment()
	{
		int numOfClusterer = 5;
		int ensembleCluster = 5;
		TagClusterEnsemble ensemble = new TagClusterEnsemble(m_NumOfCandidateTags, m_RecordsForEachTag, ensembleCluster, m_Sequence, numOfClusterer);
		Vector<String>[][] clusters = ensemble.doEnsemble();
		
		//	insert into database
		Connection conn = DBConnect.connect(m_ResultUrl, m_Usr, m_Pwd);
		Statement stmt = null;
		
		final String tableName = "tag_clustering_ensemble";
		Vector<String> targetTags = ensemble.getTargetTags();
		
		try{
			stmt = conn.createStatement();
			
			
			for(int i = 0; i < clusters.length; ++i)	// for each target tag
			{
				String targetTag = targetTags.get(i);
				for(int j = 0; j < clusters[i].length; ++j)	// for each cluster
				{
					String tags = "";
					for(String tag : clusters[i][j])
					{
						tags += tag + " ";
					}
					String insert = "insert into tag_clustering_ensemble(target_tag, cluster_id, cluster_tags, candidate_tag_number) values('"
						+ targetTag +"'," + j + ",'" + tags + "'," + m_RecordsForEachTag + ");";
//					System.out.println("ensemble:" + insert);
					stmt.executeUpdate(insert);
//					System.out.println("For record " + (i * clusters[i].length + j) + ":" + tags);
				}
			}
			conn.close();
		} catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
	}
	
	private void SkylineExperiment()
	{
		TagSkyline skyline = new TagSkyline(m_NumOfCandidateTags, m_Sequence, m_RecordsForEachTag);
		Vector<Vector<String>>[] clusters = skyline.skylineClusteringAll();
		
		//	insert into database
		Connection conn = DBConnect.connect(m_ResultUrl, m_Usr, m_Pwd);
		Statement stmt = null;
		
		final String tableName = "tag_clustering_skyline";
		Vector<String> targetTags = skyline.getTargetTags();
		
		try{
			stmt = conn.createStatement();
			
			
			for(int i = 0; i < clusters.length; ++i)	// for each target tag
			{
				String targetTag = targetTags.get(i);
				for(int j = 0; j < clusters[i].size(); ++j)	// for each cluster
				{
					String tags = "";
					for(String tag : clusters.clone()[i].get(j))
					{
						tags += tag + " ";
					}
					String insert = "insert into tag_clustering_skyline(target_tag, cluster_id, cluster_tags, candidate_tag_number) values('"
						+ targetTag +"'," + j + ",'" + tags + "'," + m_RecordsForEachTag + ");";
//					System.out.println("skyline:" + insert);
					stmt.executeUpdate(insert);
				}
			}
			conn.close();
		} catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
	}
	
	private void doExperiment()
	{
//		KmeansExperiment();
//		ClusteringEnsembleExperiment();
//		SkylineExperiment();
	}
	
	public static void main(String[] args)
	{
		int sequence[] = {1,2,3,4,5,6,7,8};
		int numOfCluster = 5;
		int numOfCandidateTags = 50;
		int recordsForEachTag = 30;
		
		Experiment experiment = new Experiment(sequence, numOfCluster, numOfCandidateTags, recordsForEachTag);
		experiment.doExperiment();
		
/*		for(int i = 0; i < 10; ++i)
		{
			System.out.println("For experiment " + i + ":");
			numOfCandidateTags = (i + 1) * 10;
			Experiment experiment = new Experiment(sequence, numOfCluster, numOfCandidateTags, recordsForEachTag);
			experiment.doExperiment();
		}*/
	}
	
}
